import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fluttertoast/fluttertoast.dart';

enum ToastType {
  normal,
  success,
  failure,
}

class ToastUtils {
  static showActivity([String msg = "正在加载..."]) async {
    EasyLoading.instance..loadingStyle = EasyLoadingStyle.dark;
    await EasyLoading.show(status: msg);
  }

  static dismissActivity() async {
    await EasyLoading.dismiss();
  }

  static showToast(String msg) async {
    _show(msg);
  }

  static showSuccess(String msg) async {
    _show(msg, type: ToastType.success);
  }

  static showError(String msg) async {
    _show(msg, type: ToastType.failure);
  }

  static _show(String msg, {ToastType type = ToastType.normal}) async {
    switch (type) {
      case ToastType.normal:
        await EasyLoading.showToast(msg);
        break;
      case ToastType.success:
        EasyLoading.instance
          ..loadingStyle = EasyLoadingStyle.custom
          ..backgroundColor = Colors.green;
        await EasyLoading.showSuccess(msg);
        break;
      case ToastType.failure:
        EasyLoading.instance
          ..loadingStyle = EasyLoadingStyle.custom
          ..backgroundColor = Colors.red;
        await EasyLoading.showError(msg);
        break;
    }
  }

  // static showSuccess(BuildContext context, String message) {
  // _showToast(context, message: message, icon: Icons.check, color: Colors.greenAccent);
  // }

  // static showError(BuildContext context, String message) {
  // _showToast(context, message: message, icon: Icons.warning, color: Colors.redAccent);
  // }

  // ignore: unused_element
  static _showToast(
    BuildContext context, {
    required String message,
    IconData? icon,
    Color color = Colors.black87,
  }) {
    if (message.trim().isEmpty) return;

    FToast ftoast = FToast();
    ftoast.init(context);

    Widget toast = _getCustomWidget(message, icon, color);

    ftoast.showToast(
      child: toast,
      gravity: ToastGravity.CENTER,
      toastDuration: Duration(seconds: 2),
    );
  }

  static Widget _getCustomWidget(
    String message,
    IconData? icon,
    Color color, {
    Axis axis = Axis.vertical,
  }) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(25.0),
        color: color,
      ),
      child: _getCustomChildWidget(message, icon, axis),
    );
  }

  static Widget _getCustomChildWidget(
    String message,
    IconData? icon,
    Axis axis,
  ) {
    if (axis == Axis.horizontal) {
      if (icon != null) {
        return Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(icon),
            SizedBox(width: 12.0),
            Text(message, style: TextStyle(fontSize: 16, color: Colors.white)),
          ],
        );
      } else {
        return Text(message, style: TextStyle(fontSize: 16, color: Colors.white));
      }
    } else {
      if (icon != null) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(icon),
            SizedBox(height: 12.0),
            Text(message, style: TextStyle(fontSize: 16, color: Colors.white)),
          ],
        );
      } else {
        return Text(message, style: TextStyle(fontSize: 16, color: Colors.white));
      }
    }
  }
}
